home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / amiga / romc_pi.a < prev    next >
Text File  |  1994-02-01  |  4KB  |  173 lines

  1.  
  2.         ;   C_PI.A
  3.         ;
  4.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.         ;
  6.         ;   Amiga startup code for PI (position independant)
  7.         ;   programs.
  8.         ;
  9.         ;   REFER TO C.A, THE ONLY DIFFERENCE IS THAT __DATA_BAS
  10.         ;   IS A (PC) RELATIVE ACCESS.  NOTE THAT -pi PROGRAMS
  11.         ;   MAY NOT BE RESIDENT
  12.         ;
  13.         ;   this startup code is used by DCC when the -pi option is
  14.         ;   used to link
  15.  
  16.         section text,code
  17.  
  18.         xref    __BSS_LEN        ; (dlink), length of BSS
  19.         xref    __DATA_BAS        ; (dlink), base of initialized data
  20.         xref    __DATA_LEN        ; (dlink), length of data
  21.  
  22.         xref    __main            ; we call _main()
  23.  
  24.         xref    _LVOSetSignal
  25.         xref    _LVOAllocMem
  26.         xref    _LVOFreeMem
  27.         xref    _LVOForbid
  28.         xref    _LVOReplyMsg
  29.  
  30.         xdef    __exit            ; we are _exit()
  31.         xdef    start
  32.         xdef    _SysBase        ; we export _SysBase
  33.         xdef    __WBMsg
  34.  
  35. MEMF_CLEAR    equ    $00000001
  36. MEMF_PUBLIC    equ    $00010000
  37.  
  38. start:
  39.         movem.l D2-D7/A2-A6,-(sp)
  40.  
  41.         move.l    4,A6            ; EXEC base
  42.  
  43.         move.l    sp,A2
  44.         move.l    A0,-(sp)            ; save arg for _main() call
  45.         move.l    D0,-(sp)            ; save arglen for _main() call
  46.  
  47.         ;   NEVER RESIDENTABLE
  48.  
  49.         lea    __DATA_BAS(pc),A4
  50.         lea    32766(A4),A4
  51.         sub.l    A3,A3
  52.  
  53. clrbss
  54.         ;   CLEAR BSS    &-32766(A4) + __DATA_LEN*4
  55.  
  56.         lea    -32766(A4),A0
  57.         move.l    #__DATA_LEN,D0
  58.         asl.l    #2,D0
  59.         add.l    D0,A0
  60.  
  61.         move.l    #__BSS_LEN,D0        ; longwords of bss
  62.         moveq.l #0,D1
  63.         bra    clrent
  64. clrlop        move.l    D1,(A0)+
  65. clrent        dbf    D0,clrlop
  66.         sub.l    #$10000,D0
  67.         bcc    clrlop
  68.  
  69.         move.l    A3,___MemList(A4)   ; memlist entry (if resident)
  70.         move.l    A2,__ExitSP(A4)     ; sp to restore
  71.  
  72.         moveq.l #0,D0            ; new signals
  73.         move.l    #$1000,D1        ; signal mask
  74.         jsr    _LVOSetSignal(A6)   ; clear ^C
  75.  
  76.         move.l    A6,_SysBase(A4)     ; resident segment.
  77.  
  78.         ;   Return value of 0 indicates succcess.  Flags
  79.         ;   have been set.
  80.  
  81.         jsr    __AutoInit0(pc)     ; A6 has SYSBase
  82.         bne    xfail
  83.         jsr    __AutoInit1(pc)     ; A6 has SYSBase
  84.         bne    xfail
  85.         jsr    __main(PC)
  86.  
  87.         ;   fall through to low level exit... this avoids referencing
  88.         ;   exit() if the user overides _main().
  89.  
  90. xfail
  91.         pea    0
  92.         pea    0
  93.  
  94.         ;   _EXIT()
  95.         ;
  96.         ;   since entry uses malloc we must free any incidental memory
  97.         ;   at __exit instead of _exit.
  98.         ;
  99.         ;   ReplyMsg(_WBMsg) just before returning
  100.  
  101. __exit:
  102.         move.l    _SysBase(A4),A6
  103.         jsr    __AutoExit1(pc)     ; A6 has SysBase
  104.         jsr    __AutoExit0(pc)     ; A6 has SysBase
  105.  
  106.         move.l    __ExitSP(A4),A5     ; get sp... because we might free
  107.                         ; the space taken by the variable!
  108.  
  109.         move.l    __WBMsg(A4),D6      ; D6 = WBMsg if it exists
  110.  
  111.         move.l    ___MemList(A4),D0   ; free memory
  112.         beq    ex20
  113. ex10        move.l    D0,A2
  114.         move.l    (A2),A3             ; next...
  115.  
  116.         move.l    4(A2),D0            ; bytes
  117.         move.l    A2,A1            ; ptr
  118.         jsr    _LVOFreeMem(A6)
  119.  
  120.         move.l    A3,D0            ; next...
  121.         bne    ex10
  122. ex20
  123.  
  124.         move.l    4(sp),D0            ; get exit code
  125.         move.l    A5,sp            ; restore sp
  126.  
  127.         tst.l    D6            ; reply to workbench msg if it
  128.         beq    ex30            ; exists
  129.         jsr    _LVOForbid(A6)      ; forbid through exit
  130.         move.l    D6,A1
  131.         jsr    _LVOReplyMsg(A6)
  132.  
  133. ex30
  134.         ;   FINIS, poof.
  135.  
  136. exfail
  137.         movem.l (sp)+,D2-D7/A2-A6
  138.         rts
  139.  
  140.  
  141.         ;   Base of autoinit section
  142.  
  143.         section autoinit0,code
  144. __AutoInit0:
  145.         section autoinit1,code
  146. __AutoInit1:
  147.         section autoexit0,code
  148. __AutoExit0:
  149.         section autoexit1,code
  150. __AutoExit1:
  151.  
  152.         ;   All library C code is compiled with the -S option
  153.         ;   which uses 'libdata' and 'libbss' section names,
  154.         ;   forcing library data to come before program data
  155.         ;   and library bss to come before program bss (because
  156.         ;   library data/bss sections are declared here first
  157.         ;   and sections of like name are coagulated).
  158.  
  159.         section libdata,data
  160. _Reserved    dc.l    0        ; force section to exist (dummy)
  161.  
  162.         section libbss,bss
  163.  
  164.         xdef    ___MemList    ; used by malloc/free
  165.  
  166. _SysBase    ds.l    1
  167. __ExitSP    ds.l    1
  168. __WBMsg     ds.l    1
  169. ___MemList    ds.l    1
  170.  
  171.         END
  172.  
  173.